<template>
  <div class="sound-control">
    <div class="sound-toggle" @click="toggleSound">
      <div class="sound-icon" :class="{ 'muted': !soundEnabled }">
        {{ soundEnabled ? '🔊' : '🔇' }}
      </div>
    </div>
    <div class="volume-slider" v-if="soundEnabled">
      <input 
        type="range" 
        min="0" 
        max="1" 
        step="0.1" 
        v-model="volume" 
        @input="updateVolume"
        class="volume-input"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import AudioService from '../../services/AudioService';

// 音效状态
const soundEnabled = ref(AudioService.getSoundEnabled());
const volume = ref(AudioService.getVolume());

// 切换音效开关
const toggleSound = () => {
  soundEnabled.value = AudioService.toggleSound();
};

// 更新音量
const updateVolume = () => {
  AudioService.setVolume(volume.value);
};

// 监听音量变化
watch(volume, (newVolume) => {
  AudioService.setVolume(newVolume);
});

// 组件挂载时初始化
onMounted(() => {
  // 确保状态同步
  soundEnabled.value = AudioService.getSoundEnabled();
  volume.value = AudioService.getVolume();
});
</script>

<style scoped>
.sound-control {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.1);
  padding: 8px 15px;
  border-radius: 30px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.sound-control:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.sound-toggle {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--naruto-orange), var(--naruto-red));
  transition: all 0.3s ease;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
}

.sound-toggle:hover {
  transform: scale(1.1);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.sound-icon {
  font-size: 1.2rem;
  transition: transform 0.3s ease;
}

.sound-icon.muted {
  transform: rotate(45deg);
}

.volume-slider {
  transition: opacity 0.3s ease;
}

.volume-input {
  -webkit-appearance: none;
  width: 100px;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.3);
  outline: none;
  transition: all 0.3s ease;
}

.volume-input::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: white;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.volume-input::-webkit-slider-thumb:hover {
  transform: scale(1.2);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

.volume-input::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: white;
  cursor: pointer;
  border: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.volume-input::-moz-range-thumb:hover {
  transform: scale(1.2);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .volume-input {
    width: 80px;
  }
}

@media (max-width: 480px) {
  .sound-control {
    padding: 6px 12px;
  }
  
  .sound-toggle {
    width: 35px;
    height: 35px;
  }
  
  .sound-icon {
    font-size: 1rem;
  }
  
  .volume-input {
    width: 60px;
  }
}
</style>
